package com.zhou.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zhou.dto.integral.IntegralDTO;
import com.zhou.entity.Integral;
import com.zhou.entity.IntegralDetail;
import com.zhou.mapper.IntegralMapper;
import com.zhou.service.IntegralService;
import com.zhou.utils.APPApplicationException;
import com.zhou.utils.BeanChangeUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;
import java.util.UUID;

import static com.zhou.utils.CommonCast.NO;
import static com.zhou.utils.CommonCast.YES;

/**
 * @Description : 积分
 * @Author : zyy
 * @Date: 2024/6/19  16:27
 */
@Service
@Slf4j
public class IntegralServiceImpl implements IntegralService {
    @Autowired
    private IntegralMapper integralMapper;
    @Override
    public String add(IntegralDTO dto) {
        log.info("积分增加");
        if (BeanChangeUtils.isEmpty(dto)) {
            throw new APPApplicationException("请传输正确的用户数据，请重试！");
        }
        //先去查这个用户是否有积分
        Integral integral = integralMapper.selectOne(new LambdaQueryWrapper<Integral>().eq(Integral::getUserInfoId, dto.getUserInfoId()));
        if (BeanChangeUtils.isEmpty(integral)) {
            //如果为空，说明没有获得过积分
            integral = new Integral();
            integral.setIntegralNumber(dto.getIntegralNumber());
            integral.setIntegralId(UUID.randomUUID().toString());
            integral.setUserInfoId(dto.getUserInfoId());
            integralMapper.insert(integral);
        }else {
            //不为空就修改总数
            integral.setIntegralNumber(integral.getIntegralNumber().add(dto.getIntegralNumber()));
            integralMapper.updateById(integral);
        }
        IntegralDetail integralDetail = new IntegralDetail();
        integralDetail.setDetail("新增"+dto.getIntegralNumber()+"积分");
        integralDetail.setIntegralId(integral.getIntegralId());
        integralDetail.setDetailContent(dto.getDetailContent());
        integralDetail.setIntegralDetailId(UUID.randomUUID().toString());
        integralDetail.setCreateTime(new Date());
        //总表加完需要加详情表
        Integer i = integralMapper.addDetail(integralDetail);
        if (i == NO) {
            throw new APPApplicationException("积分详情增加失败，请重试！");
        }else {
            return "积分增加成功";
        }
    }

    /**
     * 分页查询
     * @param page 1
     * @param pageSize 10
     * @return 积分集合
     */
    @Override
    public List<Integral> list(Integer page, Integer pageSize) {
        IPage<Integral> iPage = new Page<>(page,pageSize);
        LambdaQueryWrapper<Integral> queryWrapper = new LambdaQueryWrapper<>();
        List<Integral> records = integralMapper.selectPage(iPage, queryWrapper).getRecords();
        return records;
    }

    /**
     * 修改积分状态
     * @param integralId 积分id
     * @return 积分状态
     */
    @Override
    public String updateStatus(String integralId) {
        Integral integral = integralMapper.selectById(integralId);
        if (BeanChangeUtils.isEmpty(integral)) {
            throw new APPApplicationException("没有查询到积分记录");
        }
        if (integral.getIsStatus() == NO) {
            integral.setIsStatus(YES);
            integralMapper.updateById(integral);
            return "积分停用成功";
        }else {
            integral.setIsStatus(NO);
            integralMapper.updateById(integral);
            return "积分启用成功";
        }
    }
}
